package cn.biplam.front.component.view.systemManage.task;

import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.component.view.systemManage.task.process.TaskProcess;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.htmlscript.script.AjxScript.Level;

/**
 * 显示后台任务的执行信息的总页面
 *  若执行完任务后需要刷新父页面，需在提交前设置 refreshPararentCId和refreshPararentAid参数
 * @author WangSong 2009-8-1下午02:10:56
 * @version 1.0
 */
public class TaskProcessMain extends AjxResponse {

	public static final String cId = "8803";
	
	/** 进入任务状态显示 */
	public static final String aId_Process = cId+"01";
	/** 刷新任务状态 */
	public static final String aId_Process_Refresh = cId+"02";
	
	/** 弹出窗口级别*/
	public static final Level level = Level.Seventh;
	/** 任务执行完后刷新父页面的容器ID*/
	public static final String refreshPararentCId=cId+"taskCid";
	/** 任务执行完后刷新父页面的actionID*/
	public static final String refreshPararentAid=cId+"taskAid";
	
	public static final String Task_Id_Container = "taskId";
	
	/** 默认刷新间隔*/
	private int time = 1000;
	private TaskProcess tp;
	private static String taskId;
	
	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		
		if(aId_Process.equals(this.actionId)){
			return getStatus();
		}else if(aId_Process_Refresh.equals(this.actionId)){
			return refreshStatus();
		}else{
			throw new ErrorMessage(" TaskProcessMain.java,action is not found:"+this.actionId+"!");
		}
		
	}
	
	/**
	 * 获取状态
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow getStatus() throws ErrorMessage{
		tp = new TaskProcess(cId,mUser);
		taskId = params.getByKey(Task_Id_Container);
		if(taskId==null){
			taskId = recordId;
		}
		/**设置刷新间隔*/
		tp.setRefreshInterval(time);
		/**设置任务刷新完后，刷新的父页面信息*/
		return tp.get(taskId,params.getByKey("_fileId"),sHomeUrl);
	}
	
	/**
	 * 刷新状态
	 * @return 
	 * @throws ErrorMessage 
	 */
	private HtmlWindow refreshStatus() throws ErrorMessage{
		tp = new TaskProcess(cId,mUser);
		return tp.getRefresh(taskId,params.getByKey("_fileId"),sHomeUrl);
	}

}
